package org.ling.xuan.extention;

import com.intellij.openapi.project.Project;
import com.intellij.openapi.wm.ToolWindow;
import com.intellij.openapi.wm.ToolWindowFactory;
import com.intellij.ui.components.JBScrollPane;
import com.intellij.ui.content.Content;
import com.intellij.ui.content.ContentFactory;
import org.jetbrains.annotations.NotNull;
import org.ling.xuan.config.DbConfig;
import org.ling.xuan.pannel.SqlUtilPanel;
import org.ling.xuan.util.FileLogUtil;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;


public class LxSql implements ToolWindowFactory {
    private static final Lock lock = new ReentrantLock();
    private static final Map<String, SqlUtilPanel>  panelMap = new HashMap<>();

    @Override
    public void createToolWindowContent(@NotNull Project project, @NotNull ToolWindow toolWindow) {
        lock.lock();
        try {
            FileLogUtil.log("初始化sql工具栏开始:{}",project.getBasePath());

            SqlUtilPanel sqlUtilPanel = new SqlUtilPanel(project);
            ContentFactory contentFactory = ContentFactory.SERVICE.getInstance();
            Content content = contentFactory.createContent(new JBScrollPane(sqlUtilPanel), "", false);
            toolWindow.getContentManager().addContent(content);
            panelMap.put(project.getBasePath(),sqlUtilPanel);
            FileLogUtil.log("初始化sql工具栏成功!");
            DbConfig.getJdbcTemplate();
        } finally {
            lock.unlock();
        }
    }
    
    public static SqlUtilPanel getPanel(Project project){
        return panelMap.get(project.getBasePath());
    }
}

